<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
    <div class="nav">
        <span>网页聊天</span>
        <a href="/aboutProject.html">关于项目</a>
    </div>
    <div class="cover"></div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>登录</h3>

            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" name="username">
            </div>

            <div class="row">
                <span>密码</span>
                <input type="password" id="password" name="password">
            </div>
            <div class="row submit-row">
                <button id="submit">提交</button>
            </div>
            <div class="row registry-row">
                <button id="registry">注册</button>
            </div>
        </div>
    </div>
</body>


<script src="js/jquery.min.js"></script>
<script src="js/common.js"></script>

<script src="js/jq-paginator.js"></script>
<script src="js/bootstrap.min.js"></script>


<script>
    let registryButton = document.querySelector('#registry');
    registryButton.onclick = function () {
        location.href = "/registry.html";
    }

    let submitButton = document.querySelector('#submit');
    submitButton.onclick = function () {
        let username = document.querySelector('#username').value;
        let password = document.querySelector('#password').value;
        $.ajax({
            method: 'post',
            url: '/user/login',
            data: {
                username: username,
                password: password
            },
            success: function (result) {
                if (result.code == 1002) {
                    alert("输入的参数不合法!")
                } else if (result.code == 1103) {
                    alert("用户不存在或密码错误!");
                } else {
                    alert("登录成功!");
                    localStorage.setItem("user_token", result.data);
                    location.href = "/client.html";
                }
            }
        });
    }
</script>

</html>